package com.heatingcharge.service.base.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.entity.base.NetContract;
import com.heatingcharge.entity.charge.ChargePlan;
import com.heatingcharge.mapper.base.NetContractMapper;
import com.heatingcharge.param.NetContractParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.INetContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-12-27
 */
@Service
public class NetContractServiceImpl extends ServiceImpl<NetContractMapper, NetContract> implements INetContractService {

    @Override
    public IPage<NetContract> getNetContracts(Page page, NetContract netContract) {
        QueryWrapper<NetContract> queryWrapper=new QueryWrapper<>();
        if (!StrUtil.isEmpty(netContract.getContNum())){
            queryWrapper.eq("cont_num",netContract.getContNum());
        }
        if (!StrUtil.isEmpty(netContract.getContName())){
            queryWrapper.like("cont_name",netContract.getContNum());
        }
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public boolean save(NetContractParam netContractParam) {
        if(checkExist(netContractParam.getContNum(),null)){
            throw new BaseException(ResponseCodeEnum.CONTRACT_EXIST.getMessage(),ResponseCodeEnum.CONTRACT_EXIST.getCode());
        }
        NetContract netContract=NetContract.builder()
                .contNum(netContractParam.getContNum())
                .contName(netContractParam.getContName())
                .owner(netContractParam.getOwner())
                .partB(netContractParam.getPartB())
                .netRange(netContractParam.getNetRange())
                .area(netContractParam.getArea())
                .unitPrice(netContractParam.getUnitPrice())
                .totalCost(netContractParam.getTotalCost())
                .accepted(new BigDecimal(0))
                .reduction(new BigDecimal(0))
                .arrears(netContractParam.getTotalCost())
                .signDate(netContractParam.getSignDate())
                .startDate(netContractParam.getStartDate())
                .endDate(netContractParam.getEndDate())
                .contactPer(netContractParam.getContactPer())
                .contactNum(netContractParam.getContactNum())
                .remark(netContractParam.getRemark())
                .build();

        return baseMapper.insert(netContract)>0;
    }

    @Override
    public boolean update(NetContractParam netContractParam) {
        
        if(checkExist(netContractParam.getContNum(),netContractParam.getId())){
            throw new BaseException(ResponseCodeEnum.CONTRACT_EXIST.getMessage(),ResponseCodeEnum.CONTRACT_EXIST.getCode());
        }
        NetContract oldObj = baseMapper.selectById(netContractParam.getId());
        if (ObjectUtil.isNull(oldObj)){
            throw new BaseException(ResponseCodeEnum.CONTRACT_NON_EXIST.getMessage(),ResponseCodeEnum.CONTRACT_NON_EXIST.getCode());
        }
        NetContract newObj=NetContract.builder()
                .id(oldObj.getId())
                .contNum(netContractParam.getContNum())
                .contName(netContractParam.getContName())
                .owner(netContractParam.getOwner())
                .partB(netContractParam.getPartB())
                .netRange(netContractParam.getNetRange())
                .area(netContractParam.getArea())
                .unitPrice(netContractParam.getUnitPrice())
                .totalCost(netContractParam.getTotalCost())
//                .accepted(netContractParam.getAccepted())
//                .reduction(netContractParam.getReduction())
//                .arrears(netContractParam.getArrears())
                .signDate(netContractParam.getSignDate())
                .startDate(netContractParam.getStartDate())
                .endDate(netContractParam.getEndDate())
                .contactPer(netContractParam.getContactPer())
                .contactNum(netContractParam.getContactNum())
                .remark(netContractParam.getRemark())
                .build();
        return baseMapper.updateById(newObj)>0;
    }

    @Override
    public boolean charge(NetContractParam netContractParam) {
        if(checkExist(netContractParam.getContNum(),netContractParam.getId())){
            throw new BaseException(ResponseCodeEnum.CONTRACT_EXIST.getMessage(),ResponseCodeEnum.CONTRACT_EXIST.getCode());
        }
        NetContract oldObj = baseMapper.selectById(netContractParam.getId());
        if (ObjectUtil.isNull(oldObj)){
            throw new BaseException(ResponseCodeEnum.CONTRACT_NON_EXIST.getMessage(),ResponseCodeEnum.CONTRACT_NON_EXIST.getCode());
        }
        JwtUser user = SecurityUtil.getUser();
        NetContract newObj=NetContract.builder()
                .id(oldObj.getId())
                .accepted(oldObj.getAccepted().add(netContractParam.getChargeCount()))
                .reduction(netContractParam.getReduction())
                .arrears(netContractParam.getArrears())
                .chargeModeId(netContractParam.getChargeModeId())
                .payee(netContractParam.getPayee())
                .chargeDate(new Date())
                .build();
        return baseMapper.updateById(newObj)>0;
    }

    /**
     * 系统中是否存在指定合同编号的记录
     * @param contNum
     * @param id
     * @return
     */
    private boolean checkExist(String contNum, Integer id) {
        QueryWrapper<NetContract> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cont_num",contNum);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return this.list(queryWrapper).size()>0;
    }
}
